<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
%>
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="pragma" content="no-cache"/>
    <meta http-equiv="cache-control" content="no-cache"/>
    <meta http-equiv="expires" content="0"/>
    <title>用户列表</title>
    <link rel="stylesheet" href="<%=path %>/skin/default/css/button.css" type="text/css"/>
    <link rel="stylesheet" href="<%=path %>/scripts/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css"/>

    <script type="text/javascript" src="<%=path %>/scripts/jquery/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="<%=path %>/scripts/zTree/jquery.ztree.core-3.0.min.js"></script>
    <script type="text/javascript" src="<%=path %>/scripts/zTree/jquery.ztree.excheck-3.0.js"></script>
    <script type="text/javascript" src="<%=path %>/scripts/zTree/jquery.ztree.buttons.js"></script>
    <script type="text/javascript">
        $(function () {
            $.ajaxSetup({cache: false, async: false});
            var setting = {
                check: {
                    enable: true,
                    chkStyle: "radio",
                    radioType: "all"
                },
                view: {
                    dblClickExpand: false
                },
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onClick: zTreeOnClick,
                    onCheck: zTreeonCheck
                }
            };
            var dataMaker = function () {
                var nodes = [];
                $.getJSON("<%=path %>/org/List",function(data){
                    if(data != null){
                        $.each(data,function(i,item){
                            var h = {id:item.id,name:item.orgAlias,code:item.id,open:false,nocheck: true,icon:"<%=path %>/scripts/zTree/css/zTreeStyle/img/diy/1_close.png"};
                            nodes.push(h);
                        });
                    }
                });
                $.getJSON("<%=path %>/dept/List",function(data){
                    if(data != null){
                        $.each(data,function(i,item){
                            var d = {id:item.id,pId:item.defaultOrg,name:item.deptAlias,nocheck: true,code:"dept"};
                            nodes.push(d);
                        });
                    }
                });
                $.getJSON("<%=path %>/user/List",function(data){
                    if(data != null){
                        var p = {id:"0",name:"其他",open:false,nocheck: true,icon:"<%=path %>/scripts/zTree/css/zTreeStyle/img/diy/1_close.png"};
                        nodes.push(p);
                        $.each(data,function(i,item){
                            var defaultDept = item.defaultDeptCode;
                            if(defaultDept != null){
                                var arrs = defaultDept.split(",");
                                $.each(arrs,function(i,o){
                                    var d = {id:item.id,pId:o,name:item.realName+"["+item.account+"]",code:"user"};
                                    nodes.push(d);
                                });
                            }else{
                                var d = {id:item.id,pId:"0",name:item.realName+"["+item.account+"]",code:"user"};
                                nodes.push(d);
                            }
                        });
                    }
                });
                return nodes;
            };
            var zNodes = dataMaker();

            function zTreeOnClick(event, treeId, treeNode) {
                if (!treeNode.isParent) {
                    var zTree = $.fn.zTree.getZTreeObj(treeId);
                    zTree.checkNode(treeNode, !treeNode.checked, null, true);
                    return false;
                }
            }

            function zTreeonCheck(event, treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj(treeId);
                var nodes = zTree.getCheckedNodes(true);
                $.each(nodes, function (key, item) {
                    $("#node").val(item.id);
                    $("#nodeText").val(item.name);
                });
            }

            $(document).ready(function () {
                $.fn.zTree.init($("#companyUser"), setting, zNodes);
                var opts = {
                    btnBind: "btnBind",
                    closeBtn: "btnClose",
                    node: "node",
                    code:"defaultCompUserId",
                    name:"defaultCompUserName"
                };
                closeClick(opts);
                bindUserClick(opts);
            });
        });
    </script>
</head>
<body>
<div class="content_wrap">
    <ul id="companyUser" class="ztree"></ul>
</div>
<div class="bottomBar">
    <div class="fr" style="margin-right: 10px;">
        <input type="hidden" id="node" name="node" value=""/>
        <input type="hidden" id="nodeText" name="nodeText" value=""/>
        <input type="button" id="btnBind" name="btnBind" class="btn btn82 btn_save_bottom" value="确 定"/>
        <input type="button" id="btnClose" name="btnClose" class="btn btn82 btn_close" value="关 闭"/>
    </div>
</div>
</body>
</html>